declare let selectIsRob: any;
declare let setSubmit: any;
module MC.Order {
	export class AddOrder {
		public constructor() {
		}
		private id: string = '';
		private data: any = null;


		private tag:MC.Order.Table=new MC.Order.Table($('#addOrderDetail2'));
		private costlist:MC.Order.Table=new MC.Order.Table($('#costlist'));
		public Create() {
			this.tag.init();
			this.costlist.hangText=false;
			this.costlist.lieText='自定义成本标题';
			this.costlist.init();
			this.createInit();
			MC.apiD('getNewOrder', {id:MC.get('id')}, (json: any) => {
				this.id = json.id;
				$('#orderID').val(this.id);
				this.data = json;
				if (json.is_rob == MC.DEL.OrderBaleModel.is_rob_putong) {
					selectIsRob(2);
					$('[name=isRob][value=2]').click();
				} else if (json.is_rob == MC.DEL.OrderBaleModel.is_rob_qiangdan) {
					selectIsRob(1);
					$('[name=isRob][value=1]').click();
				}

				if (json.is_fabric == 0) {
					setSubmit(0);
					$('[name=isFabric][value=0]').click();
				} else if (json.is_fabric == 1) {
					setSubmit(1)
					$('[name=isFabric][value=1]').click();
				}
				$('[name=status][value=' + json.status + ']').click();


				$('[name=name]').val(json.name);
				$('[name=styleNo]').val(json.styleNo);
				$('[name=styleName]').val(json.styleName);
				$('[name=scence]').val(json.scence);
				$('[name=num]').val(json.num);

				$('[name=money]').val(json.money);
				$('[name=bond]').val(json.bond);
				$('[name=period]').val(json.period);
				$('[name=material]').val(json.material);
				$('[name=tech]').val(json.tech);
				$('[name=design]').val(json.design);

				$('[name=style]').val(json.style);
				$('[name=orderType]').val(json.orderType);
				$('[name=detail]').val(json.detail);

				if(json.imgDetails&&json.imgDetails!=''){
					let list= JSON.parse(json.imgDetails);
					for(let i=0;i<list.length;i++){
						let li=list[i];
						this.addPictureDiv('.pictureDivTable',li.name,li.src);
					}
				}

				if (json.proType == 1) {
					$('[name=proType][value=1]').click();
				} else if (json.proType == 2) {
					$('[name=proType][value=2]').click();
				}

				$('[name=location]').val(json.location);


				if (json.proWay == 1) {
					$('[name=proWay][value=1]').click();
				} else if (json.proWay == 2) {
					$('[name=proWay][value=2]').click();
				}

				
				$('[name=cate][value=' + json.cate + ']').click();
				if (json.stuff && json.stuff !== '') {
					let stuff = JSON.parse(json.stuff);
					for (let i = 0; i < stuff.length; i++) {
						if (i > 0) {
							$('.addtuff').click();
						}
						let one = stuff[i];
						$('[name=key' + (i + 1) + ']').val(one.key);
						$('[name=value' + (i + 1) + ']').val(one.val);
					}

				}


				if (json.sizelist && json.sizelist !== '') {
					let sizelist = JSON.parse(json.sizelist);/////尺码/数量
					let NumColor = $('.NumColor');

					for (let i = 0; i < sizelist.length; i++) {
						let one = sizelist[i];
						let size = one.size;
						let color = one.color;
						let li = NumColor.find('[size=' + size + ']')[color - 1];
						if (li) {
							$(li).val(one.num);
						}
					}

				}



				if (json.deplete && json.deplete !== '') {

					let deplete = JSON.parse(json.deplete);////面辅料单耗
					//
					for (let i = 0; i < deplete.length; i++) {
						let one = deplete[i];
						if (i > 0) {
							$('.adddeplete').click();
						}
						$('[name=name' + (i + 1) + ']').val(one.name);
						$('[name=size' + (i + 1) + ']').val(one.size);
						$('[name=num' + (i + 1) + ']').val(one.num);
						$('[name=remark' + (i + 1) + ']').val(one.remark);
					}


					if (json.img != '') {
						this.setPic('imgMain', json.img);
					}
					$('.MainDivTable .deleteico').on('click', () => {
						$('.MainDivTable .file').css('background-image', '');
						$('.MainDivTable [type=hidden]').val('');
					});
				}




				if (json.imgDetails) {
					let imgs = JSON.parse(json.imgDetails);
				}
				this.OutTypes();

				/*if (imgs.a) {
					this.setPic('imga', imgs.a[0]);
				} */
				this.loadTag();
				this.costlistTag();


			});
		}


		private loadTag(){
			let sizelist=this.data.sizelist;
			//	sizelist=`{"color":"白,红,蓝","size":"S,M,L,XL,XXL,XXXL","data":[{"c":"白","s":"S","v":"1"},{"c":"红","s":"M","v":"2"},{"c":"蓝","s":"L","v":"3"},{"c":"红","s":"XL","v":"4"},{"c":"白","s":"XXL","v":"5"},{"c":"红","s":"XXXL","v":"6"}]}`;
			if(sizelist&&sizelist!==''){
				let obj=JSON.parse(sizelist);
				this.tag.setData(sizelist);
			}else{
				this.tag.sethang('白,红,蓝');
				this.tag.setlie('S,M,L,XL,XXL,XXXL');
			}
			this.tag.create();
		}

		private costlistTag(){
			let costlist=this.data.costlist;
		 	if(costlist&&costlist!==''){ 
				this.costlist.setData(costlist);
			}else{ 
				this.costlist.sethang('数值');
				this.costlist.setlie('房租,水电,税收,生活费,裁剪,缝制,校验,打眼钉扣,整烫,包装,货运,设备折旧,耗材损耗');
			}
			this.costlist.create();
		}



		/**
		 * 外发类型数据绑定
		 */
		public OutTypes(){
			let outTypeArray = this.data.outTypeStr.split(",");
				let outTypeslist = $('[name=outTypes]');
				for (let i = 0; i < outTypeslist.length; i++) {
					let one: any = outTypeslist[i];
					$(one).attr('checked', false);
					one.checked = false;
				}
				for (let i = 0; i < outTypeArray.length; i++) {
					let one: any = $('[name=outTypes][value="' + outTypeArray[i] + '"]');
					if (one[0]) {
						one.attr('checked', true);
						one[0].checked = true;
					}

				}
		}

		private createInit() {
			$('.productPictureDiv .addPic').click(() => {
				MC.msg.Pic((json: any) => {
					this.addPictureDiv('.pictureDivTable', json.name, json.src);
				}, '款式图,粘衬部位,背面图,关键部位细节,关键部位细节图');


			});
		}

		/** 为图集添加照片和标题 */
		private addPictureDiv(div: string, name: string, src: string) {
			let html = $(`<tr class="trPic">
                <td class="name">`+ name + `</td>
                <td class="right upImg imge">
                    <div class="file" style="background-image: url(`+ src + `);">
                        <input type="hidden"  name="picsrc" value="`+ src + `"/>
						<input type="file" name="img" />
                    </div>
                    <a class="deleteico"></a>
                </td>
			</tr>`);
			$(div).append(html);
			html.find('.deleteico').on('click', () => {
				html.remove();
			});
			MC.API.bindFileUp(html.find('[type=file]'),(src:string)=>{
				html.find('[name=picsrc]').val(src);
				html.find('.file').css('background-image','url('+src+')');
			});
			$('.productPictureDiv .NullPic').hide();
		}

		/*** 获取相册的图片数据 */
		private getImgDetails(){
			let list=$('.pictureDivTable .trPic');
			let listImg=[];
			for(let i=0;i<list.length;i++){
				let li=$(list[i]);
				let one={
					name:li.find('.name').text(),
					src:li.find('[name=picsrc]').val()
				};
				listImg.push(one);
			}
			return listImg;
			//return JSON.stringify(listImg);
		}

		/*** 设置图片 */
		private setPic(select: string, src: string) {
			///	console.log(src);
			let Img = $('.upImg.' + select);
			Img.find('.file').css('background-image', 'url(' + src + ')');
			Img.find('.file').append('<input type="hidden" value="' + src + '">');

		}



		/** 
		 * 加载合同数据
		 */
		public loadContract(){
			let id=MC.get('id');
			MC.api('getOrdercontract',{id:id},(json:any)=>{
				//console.log(json);
				if(json&&json.small!=''){
					$('#hideFile').val(json.small);
					$('#addOrderPage .productPicture .file').css('background-image','url('+json.small+')');
					$('#showFile').attr('src',MC.API.toBigPic(json.small));
				}
				
			});
		}
	}
}